import {
  createRouter,
  createWebHistory
}
  from "vue-router";
import Home from "../views/Home.vue";

const routes = [{
  path: '/',
  redirect: '/dashboard'
},
  {
    path: "/",
    name: "Home",
    component: Home,
    children: [{
      path: "/dashboard",
      name: "dashboard",
      meta: {title: '系统首页'},
      component: () => import("../views/Dashboard.vue")
    }, {
      path: "/building",
      name: "building",
      meta: {title: '楼栋管理'},
      component: () => import("../views/building.vue")
    }, {
      path: "/table",
      name: "basetable",
      meta: {title: '投诉建议'},
      component: () => import("../views/BaseTable.vue")
    }, {
      path: "/notice",
      name: "notice",
      meta: {title: '通知管理'}, component: () => import("../views/notice.vue")
    }, {
      path: "/notice-edit:id",
      name: "notice-edit",
      meta: {title: '业主通知详情'}, component: () => import("../views/notice-edit.vue")
    }, {
      path: "/notice-worker-edit:id",
      name: "notice-worker-edit",
      meta: {title: '员工通知详情'}, component: () => import("../views/notice-worker-edit.vue")
    }, {
      path: "/complaint",
      name: "complaint",
      meta: {title: '投诉管理'}, component: () => import("../views/complaint.vue")
    }, {
      path: "/complaint-desc:id",
      name: "complaint-desc",
      meta: {title: '投诉详情'}, component: () => import("../views/complaint-desc.vue")
    }, {
      path: "/repair",
      name: "repair",
      meta: {title: '报修管理'}, component: () => import("../views/repair.vue")
    }, {
      path: "/repair-desc:id",
      name: "repair-desc",
      meta: {title: '报修详情'}, component: () => import("../views/repair-desc.vue")
    }, {
      path: "/charts",
      name: "basecharts",
      meta: {title: '图表'},
      component: () => import("../views/BaseCharts.vue")
    }, {
      path: "/form",
      name: "baseform",
      meta: {title: '表单'},
      component: () => import("../views/BaseForm.vue")
    }, {
      path: "/role",
      name: "role",
      meta: {title: '用户管理'},
      component: () => import("../views/Role.vue")
    }, {
      path: "/family",
      name: "family",
      meta: {title: '家庭管理'},
      component: () => import("../views/family.vue")
    }, {
      path: "/family-member:id",
      name: "family-member",
      meta: {
        title: '成员详情'
      },
      component: () => import(
        "../views/family-member.vue")
    }, {
      path: "/donate",
      name: "donate",
      meta: {
        title: '鼓励作者'
      },
      component: () => import(
        /* webpackChunkName: "donate" */
        "../views/Donate.vue")
    }, {
      path: "/permission",
      name: "permission",
      meta: {
        title: '权限管理',
        permission: true
      },
      component: () => import(
        /* webpackChunkName: "permission" */
        "../views/Permission.vue")
    }, {
      path: "/i18n",
      name: "i18n",
      meta: {
        title: '国际化语言'
      },
      component: () => import(
        /* webpackChunkName: "i18n" */
        "../views/I18n.vue")
    }, {
      path: "/upload",
      name: "upload",
      meta: {
        title: '上传插件'
      },
      component: () => import(
        /* webpackChunkName: "upload" */
        "../views/Upload.vue")
    }, {
      path: "/icon",
      name: "icon",
      meta: {
        title: '自定义图标'
      },
      component: () => import(
        /* webpackChunkName: "icon" */
        "../views/Icon.vue")
    }, {
      path: '/404',
      name: '404',
      meta: {
        title: '找不到页面'
      },
      component: () => import( /* webpackChunkName: "404" */
        '../views/404.vue')
    }, {
      path: '/403',
      name: '403',
      meta: {
        title: '没有权限'
      },
      component: () => import( /* webpackChunkName: "403" */
        '../views/403.vue')
    },
      {
        path: '/test',
        name: 'test',
        meta: {
          title: '组件测试'
        },
        component: () => import('../views/Test.vue')
      }
    ]
  }, {
    path: "/login",
    name: "Login",
    meta: {
      title: '登录'
    },
    component: () => import(
      /* webpackChunkName: "login" */
      "../views/Login.vue")
  }
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
});

router.beforeEach((to, from, next) => {
  document.title = `${to.meta.title} | 小区管理信息系统`; //设置标题
  const role = localStorage.getItem('token'); //获取用户名
  if (!role && to.path !== '/login') {
    next('/login');
  }
  else if (to.meta.permission) {
    // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
    role === 'admin' ?
      next() :
      next('/403');
  }
  else if (to.matched.length == 0) {
    next({path: '/404'})
  }
  else {
    next();
  }
});

export default router;
